//
//  ConnectToKotnet.m
//  KotnetAutologin
//
//	the main class of this project
//
//  Created by Kristof Marien on 4/01/10.
//  Copyright 2010 KotnetAutologin. All rights reserved.
//

#import "ConnectToKotnet.h"

@implementation ConnectToKotnet
/**
 *  initialize
 */
- (void)awakeFromNib
{
	statusItem = [StatusItem alloc];
	[statusItem create:statusMenu];
	[self checkConnectionStatus];
	prefWindow = [PreferencesWindow alloc];
	statsWindow = [StatsWindow alloc];
	[statusItem setAction:3:@selector(showPrefWindow:)];
	[statusItem changeItem:4 :@"Statistieken"];
}
/**
 *  show preference window
 */
- (void)showPrefWindow:(id)sender
{
	[prefWindow show:sender];
}
/**
 *  show statistic window
 */
- (void)showStatsWindow:(id)sender
{
	[statsWindow show:sender];
}
/**
 *  check network status
 */
- (void)checkConnectionStatus
{
	[statusItem changeTooltip:@"Verbinden"];
	[statusItem changeItem:1:@"Verbinding maken..."];
	[statusItem changeItem:2:@"Verbinden"];
	[statusItem changeItem:4 :@"Statistieken"];
	[statusItem setAction:2 :@selector(connect:)];
	[statusItem changeIcon:@"default"];
	checkConnection = [CheckConnection alloc];
	[checkConnection isConnected:self connectedSelector:@selector(nowConnected) notconnectedSelector:@selector(nowNotConnected)];
}
/**
 *  connected to a network, check now user is logged in
 */
- (void)nowConnected
{
	[statusItem changeTooltip:@"Verbinden"];
	[statusItem changeItem:1:@"Verbinding maken..."];
	[statusItem changeItem:2:@"Verbinden"];
	[statusItem changeItem:4 :@"Statistieken"];
	[statusItem setAction:2 :@selector(connect:)];
	[statusItem changeIcon:@"default"];
	checkLoggedIn = [CheckLoggedIn alloc];
	[checkLoggedIn checkLoggedIn:self loggedInSelector:@selector(isLoggedIn) notloggedInSelector:@selector(notLoggedIn)];
}
/**
 *  not connected to a network
 */
- (void)nowNotConnected
{
	[statusItem changeTooltip:@"Fout"];
	[statusItem changeItem:1:@"Kan geen verbinding maken."];
	[statusItem changeItem:2:@"Verbinden"];
	[statusItem changeItem:4 :@"Statistieken"];
	[statusItem setAction:2 :@selector(connect:)];
	[statusItem changeIcon:@"error"];
}
/**
 *  user is logged in
 */
- (void)isLoggedIn
{
	kotnetAvailable = [KotnetAvailable alloc];
	[kotnetAvailable checkIsAvailable:self availableSelector:@selector(kotnetIsAvailable) unavailableSelector:@selector(kotnetIsUnAvailable)];
	
	[checkLoggedIn release];
}
/**
 *  check kotnet is available
 */
- (void)kotnetIsAvailable
{
	[statusItem changeTooltip:@"Verbonden"];
	[statusItem changeItem:1:@"Verbonden."];
	[statusItem changeItem:2:@"Verbreek"];
	[statusItem setAction:2 :@selector(disconnect:)];
	[statusItem setAction:4:@selector(showStatsWindow:)];
	[statusItem changeIcon:@"succeeded"];
}
/**
 *  kotnet is unavailable
 */
- (void)kotnetIsUnAvailable
{
	if ([prefWindow closeWhenKotnetUnavailable]) {
		[NSApp terminate:nil];
	}
	[statusItem changeTooltip:@"Kotnet niet toegankelijk"];
	[statusItem changeItem:1:@"Kotnet niet toegankelijk."];
	[statusItem changeItem:2:@"Verbinden"];
	[statusItem changeItem:4 :@"Statistieken"];
	[statusItem setAction:2 :@selector(connect:)];
	[statusItem changeIcon:@"error"];
}
/**
 *  user is not logged in
 */
- (void)notLoggedIn
{
	[checkLoggedIn release];
	kotnetLogin = [KotnetLogin alloc];
	[kotnetLogin login:self cannotLoginSelector:@selector(cannotLogin:) loggedInSelector:@selector(loggedIn)];
}
/**
 *  error while trying to login
 */
- (void)cannotLogin:(NSString *)data
{
	[kotnetLogin release];
	[statusItem changeTooltip:data];
	[statusItem changeItem:1:[NSString stringWithFormat:@"%@.", data]];
	[statusItem changeItem:2:@"Verbinden"];
	[statusItem changeItem:4 :@"Statistieken"];
	[statusItem setAction:2 :@selector(connect:)];
	[statusItem changeIcon:@"error"];
}
/**
 *  user is logged in
 */
- (void)loggedIn
{
	[kotnetLogin release];
	[statusItem changeTooltip:@"Verbonden"];
	[statusItem changeItem:1:@"Verbonden."];
	[statusItem changeItem:2:@"Verbreek"];
	[statusItem setAction:2 :@selector(disconnect:)];
	[statusItem setAction:4:@selector(showStatsWindow:)];
	[statusItem changeIcon:@"succeeded"];
}
/**
 *  connect now
 */
- (void)connect:(id)sender
{
	[checkConnection check:YES];
}
/**
 *  disconnect now
 */
- (void)disconnect:(id)sender
{
	kotnetLogout = [KotnetLogout alloc];
	[kotnetLogout logout:self cannotLogoutSelector:@selector(cannotLogout:) loggedoutSelector:@selector(loggedout)];
}
/**
 *  error login out
 */
- (void)cannotLogout:(NSString *)data
{
	[kotnetLogout release];
	[statusItem changeTooltip:@"Verbonden - Kan niet afmelden"];
	[statusItem changeItem:1:@"Verbonden - Kan niet afmelden."];
	[statusItem changeItem:2:@"Verbreek"];
	[statusItem setAction:2 :@selector(disconnect:)];
	[statusItem setAction:4 :@selector(showStatsWindow:)];
	[statusItem changeIcon:@"error"];
}
/**
 *  logged out succesfully
 */
- (void)loggedout
{
	[kotnetLogout release];
	[statusItem changeTooltip:@"Niet verbonden"];
	[statusItem changeItem:1:@"Niet verbonden."];
	[statusItem changeItem:2:@"Verbinden"];
	[statusItem changeItem:4 :@"Statistieken"];
	[statusItem setAction:2 :@selector(connect:)];
	[statusItem changeIcon:@"default"];
}
@end
